package com.bw.controller;

import com.bw.bean.EmpExam;
import org.springframework.data.redis.core.BoundZSetOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.Set;

@Controller
public class EmpController {

    @Resource
    private RedisTemplate<String,EmpExam> redisTemplate;

    /***
     * 跳转到添加页面
     * @return
     */
    @RequestMapping("toAdd")
    public  String toAdd(){
        return  "add";
    }

    /***
     * 添加方法
     * @param empExam
     * @return
     */
    @RequestMapping("addEmp")
    public String addEmp(EmpExam empExam){
        BoundZSetOperations<String, EmpExam> weekZset = redisTemplate.boundZSetOps("weekZset");
        weekZset.add(empExam,empExam.getScore());
        return  "redirect:list";
    }

    /**
     * 列表方法
     * 当前页  cpage
     * 分页单位 pageSize
     * @return
     */
    @RequestMapping("list")
    public String list(Model model,
                       @RequestParam(defaultValue = "1") long cpage,
                       @RequestParam(defaultValue = "4") long pageSize
    ){
        BoundZSetOperations<String, EmpExam> weekZset = redisTemplate.boundZSetOps("weekZset");

        //总条数 总记录数
        Long count = weekZset.zCard();

        // 分页查询的开始和结束的位置
        long start = (cpage-1)*pageSize;
        long end = cpage * pageSize -1;
        //分页查询数据
        Set<EmpExam> empExams = weekZset.range(start, end);
        //上一页
        long prePage =  cpage == 1 ? 1 : (cpage - 1);
        //总页数  尾页
        long pageCount = count/pageSize + (count%pageSize==0?0:1);
        //下一页
        long nextPage =  cpage+1 >= pageCount ? pageCount : cpage + 1;

        model.addAttribute("zset",empExams);
        model.addAttribute("cpage",cpage);
        model.addAttribute("prePage",prePage);
        model.addAttribute("nextPage",nextPage);
        model.addAttribute("pageCount",pageCount);
        model.addAttribute("count",count);

        return  "list";
    }

    /**
     * 到修改页面
     * @return
     */
    @RequestMapping("toUpdate")
    public  String toUpdate(EmpExam empExam,Model model){
        model.addAttribute("emp",empExam);

        //查出集合中所有的数据
        BoundZSetOperations<String, EmpExam> weekZset = redisTemplate.boundZSetOps("weekZset");
        Set<EmpExam> range = weekZset.range(0, -1);
        //获取到要修改元素的索引
        //long rank = 0;
        for (EmpExam exam : range) {
            if(empExam.getName().equals(exam.getName())){
                //要修改的元素删除
                weekZset.remove(exam);
            }
        }

        return  "update";
    }

    @RequestMapping("delEmp")
    public  String delEmp(EmpExam empExam,Model model){

        //查出集合中所有的数据
        BoundZSetOperations<String, EmpExam> weekZset = redisTemplate.boundZSetOps("weekZset");
        Set<EmpExam> range = weekZset.range(0, -1);
        //获取到要修改元素的索引
        //long rank = 0;
        for (EmpExam exam : range) {
            if(empExam.getName().equals(exam.getName())){
                //要修改的元素删除
                weekZset.remove(exam);
            }
        }
        return  "redirect:list";
    }

}
